package com.tbit.uqbike.client.service.impl;

import com.tbit.uqbike.client.constant.AccountConfigExtConstant;
import com.tbit.uqbike.client.constant.MapConstant;
import com.tbit.uqbike.client.dao.core.AccountConfigExtDao;
import com.tbit.uqbike.client.dao.core.ForbiddenDao;
import com.tbit.uqbike.client.pojo.Point;
import com.tbit.uqbike.client.pojo.newEntity.AccountConfigExt;
import com.tbit.uqbike.client.pojo.newEntity.FleetMachineBound;
import com.tbit.uqbike.client.pojo.newEntity.Forbidden;
import com.tbit.uqbike.client.service.CalibrateService;
import com.tbit.uqbike.client.service.ForbiddenService;
import com.tbit.uqbike.client.util.GeoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
 * @Author: chen
 * @Description:
 * @Date: 创建于 9:24 2022/12/23
 */
@Service
public class ForbiddenServiceImpl implements ForbiddenService {

    @Autowired
    private ForbiddenDao forbiddenDao;
    @Autowired
    private CalibrateService calibrateService;
    @Autowired
    private AccountConfigExtDao accountConfigExtDao;

    @Override
    public List<Forbidden> getByArea(Integer accountId, double lo, double la) {
        int radius = Optional.ofNullable(accountConfigExtDao.selectByAccountIdAndKey(accountId, AccountConfigExtConstant.FORBIDDEN_SCOPE))
                .map(AccountConfigExt::getParamValue)
                .map(Integer::parseInt)
                .orElse(500);

        // 配置值为0表示查询整个运营区的数据
        if (radius == 0) {
            return forbiddenDao.getByAccountId(accountId);
        }

        Point[] rect = GeoUtil.getBoundingBox(lo, la, radius);

        return forbiddenDao.getByArea(accountId, rect[0].getX(), rect[0].getY(), rect[1].getX(), rect[1].getY());
    }

    @Override
    public List<Forbidden> getByUserCode(String userCode, Integer mapType) {
        List<Forbidden> forbiddens = forbiddenDao.getByUserCode(userCode);
        if (!forbiddens.isEmpty()) {
            for (Forbidden forbidden : forbiddens) {
                calibrateService.calForbidden(forbidden, MapConstant.MAP_ORI, mapType);
            }
        }
        return forbiddens;
    }

    @Override
    public List<Forbidden> getByAccountId(Integer accountId, Integer mapType) {
        List<Forbidden> forbiddens = forbiddenDao.getByAccountId(accountId);
        if (!forbiddens.isEmpty()) {
            for (Forbidden forbidden : forbiddens) {
                calibrateService.calForbidden(forbidden, MapConstant.MAP_ORI, mapType);
            }
        }
        return forbiddens;
    }

    @Override
    public FleetMachineBound getFleetId(Integer machineId) {
        return forbiddenDao.getFleetId(machineId);
    }

    @Override
    public boolean checkInForbidden(String userCode, Point point) {
        List<Forbidden> forbiddens = forbiddenDao.getByUserCode(userCode);
        if (!forbiddens.isEmpty()) {
            for (Forbidden forbidden : forbiddens) {
                List<Point> geoPoints = GeoUtil.getPoints(forbidden.getPoints());
                if (GeoUtil.IsPtInPoly(point, geoPoints)) {
                    return true;
                }
            }
        }
        return false;
    }
}
